#!/usr/bin/env node

/**
 * 油价抓取功能测试脚本
 * 使用方法: node scripts/test-oil-price.js
 */

const axios = require('axios');
const cheerio = require('cheerio');

async function testOilPriceScraping() {
    console.log('🚀 开始测试油价抓取功能...\n');

    const url = 'https://data.eastmoney.com/cjsj/yjtz/default.html';

    try {
        console.log('📡 正在抓取东方财富网油价数据...');

        // 设置请求头，模拟真实浏览器
        const headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'Cache-Control': 'max-age=0',
            'Referer': 'https://data.eastmoney.com/'
        };

        // 添加随机延迟
        const delay = Math.random() * 2000 + 1000;
        console.log(`⏰ 等待 ${Math.round(delay)}ms 避免被反爬...`);
        await new Promise(resolve => setTimeout(resolve, delay));

        const response = await axios.get(url, {
            headers,
            timeout: 30000,
            validateStatus: (status) => status === 200
        });

        console.log('✅ 成功获取页面数据');
        console.log(`📊 响应状态: ${response.status}`);
        console.log(`📏 数据大小: ${(response.data.length / 1024).toFixed(2)} KB\n`);

        // 解析HTML
        const $ = cheerio.load(response.data);
        const oilPrices = [];

        console.log('🔍 开始解析油价数据...\n');

        // 查找表格
        const tables = $('table');
        console.log(`📋 找到 ${tables.length} 个表格`);

        // 解析表格数据
        $('table tr').each((i, row) => {
            if (i === 0) return; // 跳过表头

            const cells = $(row).find('td');
            if (cells.length >= 5) {
                const province = $(cells[0]).text().trim();
                const price92Text = $(cells[1]).text().trim();
                const price95Text = $(cells[2]).text().trim();
                const price98Text = $(cells[3]).text().trim();
                const price0Text = $(cells[4]).text().trim();

                // 解析价格数据
                const price92 = parsePrice(price92Text);
                const price95 = parsePrice(price95Text);
                const price98 = parsePrice(price98Text);
                const price0 = parsePrice(price0Text);

                if (price92 > 0 && price95 > 0 && price98 > 0 && price0 > 0) {
                    oilPrices.push({
                        province,
                        price92,
                        price95,
                        price98,
                        price0
                    });

                    console.log(`📍 ${province}: 92# ${price92} | 95# ${price95} | 98# ${price98} | 0# ${price0}`);
                }
            }
        });

        console.log(`\n📈 抓取结果统计:`);
        console.log(`   - 总记录数: ${oilPrices.length}`);
        console.log(`   - 省份数量: ${new Set(oilPrices.map(p => p.province)).size}`);

        if (oilPrices.length > 0) {
            const avg92 = calculateAverage(oilPrices.map(p => p.price92));
            const avg95 = calculateAverage(oilPrices.map(p => p.price95));
            const avg98 = calculateAverage(oilPrices.map(p => p.price98));
            const avg0 = calculateAverage(oilPrices.map(p => p.price0));

            console.log(`   - 平均价格:`);
            console.log(`     * 92#: ${avg92.toFixed(2)} 元/升`);
            console.log(`     * 95#: ${avg95.toFixed(2)} 元/升`);
            console.log(`     * 98#: ${avg98.toFixed(2)} 元/升`);
            console.log(`     * 0#: ${avg0.toFixed(2)} 元/升`);
        }

        console.log('\n🎉 油价抓取测试完成！');

    } catch (error) {
        console.error('\n❌ 油价抓取测试失败:', error.message);

        if (error.response) {
            console.error(`   状态码: ${error.response.status}`);
            console.error(`   响应头:`, error.response.headers);
        }

        console.error('\n💡 可能的原因:');
        console.error('   1. 网络连接问题');
        console.error('   2. 网站反爬虫机制');
        console.error('   3. 页面结构发生变化');
        console.error('   4. 需要验证码或登录');
    }
}

/**
 * 解析价格字符串
 */
function parsePrice(priceText) {
    if (!priceText) return 0;

    // 移除价格中的非数字字符，只保留数字和小数点
    const price = parseFloat(priceText.replace(/[^\d.]/g, ''));
    return isNaN(price) ? 0 : price;
}

/**
 * 计算平均值
 */
function calculateAverage(prices) {
    if (prices.length === 0) return 0;
    const sum = prices.reduce((acc, price) => acc + price, 0);
    return sum / prices.length;
}

// 运行测试
if (require.main === module) {
    testOilPriceScraping().catch(console.error);
}

module.exports = { testOilPriceScraping };
